package com.example.mail.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * ClassName: MailService
 * Description:
 * date: 2021/8/25 11:13
 *
 * @author 17878
 * @since JDK 1.8
 */
@Service
public class MailService {
    @Resource
    private JavaMailSender javaMailSender;

    private String from="1787812757@qq.com";

    /**
     * 简单邮件发送
     * @param to
     * @param subject
     * @param content
     */
    public void mail(String to,String subject,String content){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from);
        message.setText(content);
        message.setTo(to);
        javaMailSender.send(message);
    }

    /**
     * html邮件发送
     * @param to
     * @param subject
     * @param content
     * @throws MessagingException
     */
    public void sendHtmlMail(String to,String subject,String content) throws MessagingException {//html邮件发送
        MimeMessage message=javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        javaMailSender.send(message);
    }

    /**
     * 带附件的邮件发送
     * @param to
     * @param subject
     * @param conent
     * @param path
     * @throws MessagingException
     */
    public void sendAttachmentsMail(String to,String subject,String conent,String path) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(conent);

        FileSystemResource file = new FileSystemResource(new File(path));
        String fileName=file.getFilename();
        helper.addAttachment(fileName,file);
        javaMailSender.send(message);
    }

    /**
     * 发送图片邮件
     * @param to
     * @param subject
     * @param conent
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */
    public void sendInlinResourceMail(String to,String subject,String conent,String rscPath,String rscId) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message, true);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(conent,true);

        FileSystemResource rsc = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,rsc);
        javaMailSender.send(message);
    }
}
